//
//  rankController.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/1.
//  Copyright © 2015年 周莹. All rights reserved.
//

#define RankWidth [UIScreen mainScreen].bounds.size.width
#define RankHeight [UIScreen mainScreen].bounds.size.height

#import "UIImageView+WebCache.h"
#import "recommendHelper.h"
#import "recommendModel.h"
#import "rankController.h"
#import "recommendCell.h"
#import "MJRefresh.h"
#import "songListModel.h"
#import "RadioDetailViewController.h"

@interface rankController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *bigTableView;


@end

@implementation rankController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[recommendHelper sharedInstancetype]downLoadrankDataFinished:^{
        [self.bigTableView reloadData];
    }withIndex:self.index];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.bigTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, RankWidth, RankHeight - 116) style:UITableViewStylePlain];
   // self.bigTableView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.bigTableView];
    self.bigTableView.delegate = self;
    self.bigTableView.dataSource = self;
    
    [self.bigTableView registerClass:[recommendCell class] forCellReuseIdentifier:@"flag"];
    
    [self.bigTableView addHeaderWithTarget:self action:@selector(headerHeightDidClicked)];
    [self.bigTableView headerBeginRefreshing];
    [self.bigTableView addFooterWithTarget:self action:@selector(footerefreshing)];
    
    self.bigTableView.headerPullToRefreshText = @"下拉可以刷新";
    self.bigTableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.bigTableView.headerRefreshingText = @"正在帮你刷新中,不客气";
    
    self.bigTableView.footerPullToRefreshText = @"- The End -";
    self.bigTableView.footerReleaseToRefreshText = @"已经到头了";
    self.bigTableView.footerRefreshingText = @"- The End -";


}

-(void)headerHeightDidClicked
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        if ([recommendHelper sharedInstancetype].rankArray == nil) {
          
            // 刷新表格
            [self.bigTableView reloadData];
            
        }
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.bigTableView headerEndRefreshing];
    });}

-(void)footerefreshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        [[recommendHelper sharedInstancetype]downLoadrankDataFinished:^{
            [self.bigTableView reloadData];
            
            self.index = (self.bigTableView.contentOffset.y - 1515) / 2000 ;
            
            self.index = self.index + 1;

        } withIndex:self.index];
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.bigTableView footerEndRefreshing];
       
        
    });
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [recommendHelper sharedInstancetype].rankArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    recommendModel *model = [recommendHelper sharedInstancetype].rankArray[indexPath.row];

    recommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flag" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.titleLabel.text = model.title;
    cell.introLabel.text = model.intro;
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
    cell.userLabel.text = [NSString stringWithFormat:@"%@", model.playsCounts];
    cell.setLabel.text = [NSString stringWithFormat:@"%@集", model.tracksCounts];
   // cell.introLabel.text = model.intro;
  //  [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
    
    if ([cell.userLabel.text isEqualToString:@"0"]) {
        cell.userImage.hidden = YES;
        cell.userLabel.text = @"";
        cell.setImage.frame = CGRectMake(95, 78, 10, 10);
        cell.setLabel.frame = CGRectMake(112, 75, 65, 15);
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    recommendModel *model = [recommendHelper sharedInstancetype].rankArray[indexPath.row];
    RadioDetailViewController *radio = [[RadioDetailViewController alloc] init];
    [self.navigationController pushViewController:radio animated:YES];

    radio.albumId = model.albumId;
    radio.position = [NSString stringWithFormat:@"%ld",indexPath.row];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
